/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   演示ATIM_CH1/2/3/4/5/6触发VC的BLANK
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J21、J22, 短接J28
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_vc.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
ATIM_HandleTypeDef hatim;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void VC2_Init(void);
static void ATIM_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 系统时钟初始化
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);
    HAL_Delay(3000);

    // SWD用作IO
    HAL_SysCtrl_SWD2GPIO();

    // 初始化VC2
    VC2_Init();

    // 初始化ATIM
    ATIM_Init();

    // 请向VC2_IN2(PB02)提供 > Vcore(约1.6V) 的电压
    // 请通过示波器查看VC2_OUT(PA04)输出的波形
    // 请通过示波器查看ATIM_CH各通道输出的波形
    // 可以看到当ATIM各通道比较匹配时触发VC2产生BLANK, 此时VC2_OUT输出低电平
    while (1)
    {
        ;
    }
}


/*******************************************************************************
 * @brief  VC2初始化函数
 * @param  None
 * @retval None
 * @note
 */
static void VC2_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    VC_InitTypeDef   VC_InitStruct;

    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));
    MemClr(&VC_InitStruct,   sizeof(VC_InitStruct));

    // 使能外设时钟
    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_VC_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置 VC2_INP 和 VC2_INN
    AFx_VC2IN2_PB02();                                                // VC2_IN2管脚配置
    HAL_VCReference_Enable(VCREF_VIN_VCAP, 8 - 1);                    // 分压比为8/8，输出电压为Vcore * 8 / 8

    //------------------------------------------------------------
    // 配置 VC2_OUT
    AFx_VC2OUT_PA04();
    GPIO_InitStruct.Pins = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 初始化 VC2
    VC_InitStruct.PChannel   = VC_PCHANNEL_IN2;                       // VC正端 = IN2
    VC_InitStruct.NChannel   = VC_NCHANNEL_VCREF;                     // VC负端 = 内置VC参考源输出
    VC_InitStruct.WindowEn   = VC_WINDOW_DISABLE;                     // 禁止窗口比较功能
    VC_InitStruct.Polarity   = VC_POLARITY_POSITIVE;                  // 正端大于负端时输出高电平
    VC_InitStruct.Hysteresis = VC_HYS_25mV;                           // 配置迟滞为25mV
    VC_InitStruct.Response   = VC_RESP_FAST;                          // 配置响应速度为快
    VC_InitStruct.FilterClk  = VC_FILTERCLK_PCLK;                     // 滤波时钟选择PCLK
    VC_InitStruct.FilterTime = VC_FILTERTIME_FLTCLK1_N8;              // 滤除宽度小于7个LSI时钟的干扰信号
    HAL_VC_Init(SYM_VC2, &VC_InitStruct);                             // 初始化VC2

    // 配置VC2的BLANK
    uint32_t tmpBlankSrc = VC_BLANKSRC_ATIM_OC1REFC | VC_BLANKSRC_ATIM_OC2REFC |
                           VC_BLANKSRC_ATIM_OC3REFC | VC_BLANKSRC_ATIM_OC4REFC |
                           VC_BLANKSRC_ATIM_OC5REFC | VC_BLANKSRC_ATIM_OC6REFC;
    HAL_VC_Blank_Config(SYM_VC2, tmpBlankSrc, 6 ,VC_BLANKLEVEL_LOW);

    HAL_VC_Enable(SYM_VC2);
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    GPIO_InitTypeDef                 GPIO_InitStruct;
    MemClr(&GPIO_InitStruct,    sizeof(GPIO_InitStruct));

    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_ATIM_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置输出端口 ATIM_CH1/2/3/4/5/6
    AFx_ATIMCH1_PB00();
    AFx_ATIMCH2_PB01();
    AFx_ATIMCH3_PA03();
    AFx_ATIMCH4_PA00();
    AFx_ATIMCH5_PB05();
    AFx_ATIMCH6_PA07();

    HAL_GPIO_WritePins(SYM_GPIOA, GPIO_PIN_0 | GPIO_PIN_3 | GPIO_PIN_7, GPIO_PIN_RESET);
    GPIO_InitStruct.Pins = GPIO_PIN_0 | GPIO_PIN_3 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    HAL_GPIO_WritePins(SYM_GPIOB, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5, GPIO_PIN_RESET);
    GPIO_InitStruct.Pins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance               = SYM_ATIM;
    hatim.Init.ClockDivision     = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode       = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler         = 48 - 1;
    hatim.Init.Period            = 700 - 1;
    hatim.Init.RepetitionCounter = 0;
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_Base_Init(&hatim);

    hatim.OcInit.OCMode       = ATIM_OCMODE_PWM2;
    hatim.OcInit.OCPolarity   = ATIM_OCPOLARITY_HIGH;
    hatim.OcInit.OCNPolarity  = ATIM_OCNPOLARITY_HIGH;
    hatim.OcInit.OCFastMode   = ATIM_OCFAST_ENABLE;
    hatim.OcInit.OCIdleState  = ATIM_OCIDLESTATE_SET;
    hatim.OcInit.OCNIdleState = ATIM_OCNIDLESTATE_SET;
    hatim.OcInit.Pulse        = 100 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_1);
    hatim.OcInit.Pulse        = 200 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_2);
    hatim.OcInit.Pulse        = 300 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_3);
    hatim.OcInit.Pulse        = 400 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_4);
    hatim.OcInit.Pulse        = 500 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_5);
    hatim.OcInit.Pulse        = 600 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_6);

    // 通道输出使能并启动ATIM
    HAL_ATIM_PWM_Start(&hatim, ATIM_CHANNEL_ALL);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
